package com.salon.server.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.server.entity.Appointment;
import com.salon.server.model.R;
import com.salon.server.model.request.AppointmentRequest;
import com.salon.server.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 * 预约信息表 前端控制器
 * </p>
 *
 * @author zooy
 * @since 2025-08-28
 */
@RestController
@RequestMapping("salon/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    /**
     * page
     */
    @PostMapping("page")
    public R page(@RequestBody AppointmentRequest request) {

        return R.success(appointmentService.page(request, new LambdaQueryWrapper<Appointment>()
                .eq(StrUtil.isNotEmpty(request.getStatus()), Appointment::getStatus, request.getStatus())
                .eq(StrUtil.isNotEmpty(request.getMemberName()), Appointment::getMemberName, request.getMemberName())
                .eq(request.getAppointmentDate() != null, Appointment::getAppointmentDate, request.getAppointmentDate())));
    }

    /**
     * add
     */
    @PostMapping("add")
    public R add(@RequestBody Appointment appointment) {

        // 通过项目id，查询项目相关的情况

        appointmentService.saveAppointment(appointment);
        return R.success();
    }

    /**
     * list
     */
    @PostMapping("list")
    public R list(@RequestBody AppointmentRequest request) {
        return R.success(appointmentService.list(request, new LambdaQueryWrapper<Appointment>().orderByDesc(Appointment::getCreatedAt)));
    }

    /**
     * delete
     */
    @PostMapping("cancel")
    public R cancel(@RequestBody AppointmentRequest request) {
        return R.success(appointmentService.removeById(request.getId()));
    }

     /**
      * update
      */
     @PostMapping("update")
     public R update(@RequestBody Appointment appointment) {

         return R.success(appointmentService.updateById(appointment));
     }

}
